package cn.yuan.xiaoyu.jokemodule.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.utils.ListUtils;
import cn.yuan.xiaoyu.jokemodule.bean.JokeListBean;

/**
 * Created by yukuoyuan on 2017/2/27.
 * 这是一个笑话列表的适配器
 */

public class JokeListAdapter extends RecyclerView.Adapter<JokeListAdapter.MyHolder> {
    List<JokeListBean.ResultBean.DataBean> data;

    public JokeListAdapter(List<JokeListBean.ResultBean.DataBean> data) {
        this.data = data;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_joke_list, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        SpannableStringBuilder span = new SpannableStringBuilder("缩进" + data.get(position).content);
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        holder.tv_item_joke.setText(span);
    }

    @Override
    public int getItemCount() {
        if (ListUtils.isEmpty(data)) {
            return 0;
        }
        return data.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final TextView tv_item_joke;

        public MyHolder(View itemView) {
            super(itemView);
            tv_item_joke = (TextView) itemView.findViewById(R.id.tv_item_joke);
        }
    }
}
